package com.hissy.common.chain;

/**
 * Created by zhangyanfei on 2018/12/27.
 *
 * @author zhangyanfei
 * @date 2018/12/27
 */
public class Scope {
    public static final Scope LINE = new Scope(0);

    public static final Scope A_SCOPE = new Scope(1);

    public static final Scope B_SCOPE = new Scope(2);

    public static final Scope C_SCOPE = new Scope(2);
    public int scope;

    public Scope(int scope) {
        this.scope = scope;
    }

    public static class ScopeBuilder {
        int scope = LINE.scope;

        /**
         * 扩充scope范围
         *
         * @param extendedScope
         * @return
         */
        public ScopeBuilder extend(Scope extendedScope) {
            this.scope = extendedScope.scope | this.scope;
            return this;
        }

        public Scope build() {
            return new Scope(this.scope);
        }
    }

    /**
     * 是否当前scope被source包含
     *
     * @param source
     * @return
     */
    public boolean isExtended(Scope source) {
        if (source == null) {
            return false;
        }
        return (this.scope & source.scope) == this.scope;
    }
}
